package com.xc.hotelmgr.webapi.modules.mq.service.impl;


import com.xc.hotelmgr.webapi.common.utils.MqType;
import com.xc.hotelmgr.webapi.common.utils.SendMessageMgr;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtOrderChannelsEntity;
import com.xc.hotelmgr.webapi.modules.dt.entity.StoreEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtOrderChannelsService;
import com.xc.hotelmgr.webapi.modules.dt.service.StoreService;
import com.xc.hotelmgr.webapi.modules.ht.entity.HtLogicRoomEntity;
import com.xc.hotelmgr.webapi.modules.mq.dto.*;
import com.xc.hotelmgr.webapi.modules.op.entity.HtlGroupDto;
import com.xc.hotelmgr.webapi.modules.op.entity.HtlGroupEntity;
import com.xc.hotelmgr.webapi.modules.op.entity.TaskStatusEntity;
import com.xc.hotelmgr.webapi.modules.op.service.DtUpHtlService;
import com.xc.hotelmgr.webapi.modules.mq.service.HotelToMQService;
import com.xc.hotelmgr.webapi.modules.op.service.HtlGroupService;
import com.xc.hotelmgr.webapi.modules.op.service.TaskStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class HoteToMQServiceImpl implements HotelToMQService {

    private static final String STORE_HOTEL_TASK_CODE = "STOREHOTEL";

    private static final String STORE_HOTEL_TASK_NAME = "分销商店铺酒店推送";

    private static final String RP_TASK_NAME = "分销商价格库存推送";

    private static final String STORE_HOTEL_STATE_NAME = "分销商店铺已上架酒店状态变更";

    @Autowired
    private DtUpHtlService dtUpHtlService;

    @Autowired
    private SendMessageMgr sendMessageMgr;

    @Autowired
    private TaskStatusService opTaskStatusService;

    @Autowired
    private StoreService dtStoreService;

    @Autowired
    private HtlGroupService opHtlGroupService;

    @Autowired
    private DtOrderChannelsService dtOrderChannelsService;

    /**
     * 分组拉取推送（放入分组拉取推送队列）
     * @param htlGroupDto
     */
    @Async
    @Override
    public void pullSendGroup(HtlGroupDto htlGroupDto) {
        List<OpDtStoreGroup> opDtStoreGroups = opHtlGroupService.getStoreHotelByGroup(htlGroupDto);
        //分组下没有酒店
        if (opDtStoreGroups.size() <= 0) {
            return;
        }
        StoreEntity dtStore = dtStoreService.getById(htlGroupDto.getStoreId());
        String taskName = "分组酒店拉取【" + opDtStoreGroups.get(0).getGroupName() + "】";
        TaskStatusEntity opTaskStatus = new TaskStatusEntity();
        opTaskStatus.setArchive(0);
        opTaskStatus.setTaskCode("ALLHOTEL");
        opTaskStatus.setStatus(0);
        //完成数
        opTaskStatus.setCompPercent(0L);
        //总数
        opTaskStatus.setCompAmount(Long.valueOf(opDtStoreGroups.size()));
        opTaskStatus.setExcuTime(new Date());
        opTaskStatus.setTaskName(taskName);
        opTaskStatus.setStorId(htlGroupDto.getStoreId());
        opTaskStatus.setDtId(dtStore.getDtId());
        opTaskStatusService.save(opTaskStatus);
        Long taskId = opTaskStatus.getId();
        DtOrderChannelsEntity dtOrderChannelsEntity = dtOrderChannelsService.getByStoreIdAndChanel(htlGroupDto.getStoreId(), 1);
        for (OpDtStoreGroup opDtStoreGroup : opDtStoreGroups) {
            List<Long> hotelIdList = new ArrayList<>();
            Long hId = opDtStoreGroup.getHotelId();
            hotelIdList.add(hId);
            //发送消息
            TaskHotelList taskHotelList = new TaskHotelList();
            if (dtOrderChannelsEntity != null) {
                taskHotelList.setAID(Integer.valueOf(dtOrderChannelsEntity.getAccessKey()));
                taskHotelList.setSID(dtOrderChannelsEntity.getSid());
                taskHotelList.setKEY(dtOrderChannelsEntity.getSecretKey());
            }
            taskHotelList.setTaskId(taskId);
            taskHotelList.setHotelList(hotelIdList);
            taskHotelList.setStoreId(htlGroupDto.getStoreId());
            taskHotelList.setGroupId(htlGroupDto.getGroupId());
            taskHotelList.setAppKey(opDtStoreGroup.getAppKey());
            taskHotelList.setSecret(opDtStoreGroup.getSecret());
            taskHotelList.setSessionKey(opDtStoreGroup.getSessionKey());
            taskHotelList.setIsGroup(true);
            sendMessageMgr.sendMessage(MqType.HTLIDLIST, taskHotelList, "");
        }
        List<Long> endIdList = new ArrayList<>();
        Integer StoreId = htlGroupDto.getStoreId();
        TaskHotelList taskHotelList = new TaskHotelList();
        taskHotelList.setTaskId(taskId);
        taskHotelList.setStoreId(StoreId);
        taskHotelList.setHotelList(endIdList);
        taskHotelList.setGroupId(htlGroupDto.getGroupId());
        sendMessageMgr.sendMessage(MqType.HTLIDLIST, taskHotelList, "");
    }

    /**
     * 店铺酒店全部上下架
     * @param storeId
     * @param status
     */
    @Async
    @Override
    public void upDown(Integer storeId, Integer status) {

        List<Long> list = dtUpHtlService.getByStoreId(storeId);
        if(!list.isEmpty()) {
            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            Long num = Long.valueOf(list.size()); // 执行总数量

            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.save(opTaskStatus);
            Long taskId = opTaskStatus.getId();  // 任务id
            list.add(Long.parseLong("-1"));  // 结束判断标识

            for (Long hotelId : list) {
                UpDown upDown = new UpDown();
                upDown.setHotelId(hotelId);
                upDown.setStatus(status);
                upDown.setStoreId(storeId);
                upDown.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HTUPDOWN, upDown, appKey);
            }
        }
    }

    /**
     * 店铺选定酒店上下架
     * @param upDownList
     */
    @Async
    @Override
    public void upDownList(UpDownList upDownList) {
        List<Long> hotelList = upDownList.getHotelList();
        if(!hotelList.isEmpty()) {
            Integer storeId = upDownList.getStoreId();
            Integer status = upDownList.getStatus();
            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            Long num = Long.valueOf(hotelList.size()); // 执行酒店数量

            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);

            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.save(opTaskStatus);
            Long taskId = opTaskStatus.getId();
            hotelList.add(Long.parseLong("-1")); // 结束判断标识

            for (Long hotelId : hotelList) {
                UpDown upDown = new UpDown();
                upDown.setHotelId(hotelId);
                upDown.setStatus(status);
                upDown.setStoreId(storeId);
                upDown.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HTUPDOWN, upDown, appKey);
            }
        }
    }

    /**
     * 店铺酒店全部推送
     * @param storeId
     */
    @Async
    @Override
    public void sendAll(Integer storeId) {
        List<Long> list = dtUpHtlService.getByStoreId(storeId);
        if(!list.isEmpty()) {
            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            Long num = Long.valueOf(list.size());
            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.save(opTaskStatus);  // 生成新的任务
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }

    /**
     * 店铺酒店选定推送
     * @param sendAllList
     */
    @Async
    @Override
    public void sendAllList(SendAllList sendAllList) {
        List<Long> list = sendAllList.getHotelList();
        if(!list.isEmpty()) {
            int storeId = sendAllList.getStoreId();
            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            Long num = Long.valueOf(list.size());

            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.save(opTaskStatus);
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }

    @Override
    public void sendRs(SendRsDto sendRs) {
        Integer storeId = sendRs.getStoreId();
        Integer sendType = sendRs.getSendType();
        //当 sendType = 2 或者 3 时，sendId 为地区编码，当 sendId = 4 或 5时，sendId 为分组编码
        Long sendId = sendRs.getSendId();
        List<Long> list = null;
        switch (sendType){
            case 2:
            case 3:
                list = dtUpHtlService.getByAreaId(storeId,sendId);
                break;
            case 4:
            case 5:
                list = dtUpHtlService.getByGroupId(storeId,sendId);
                break;
            default:
                list = dtUpHtlService.getByStoreId(storeId);
        }

        if(!list.isEmpty()) {
            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();
            Long num = Long.valueOf(list.size());
            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(RP_TASK_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);
            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatus.setDtId(dtStore.getDtId());
            opTaskStatusService.save(opTaskStatus);  // 生成新的任务
            Long taskId = opTaskStatus.getId();
            list.add(Long.parseLong("-1"));
            for (Long hotelId : list) {
                SendAll sendAll = new SendAll();
                sendAll.setHotelId(hotelId);
                sendAll.setStoreId(storeId);
                sendAll.setTaskId(taskId);
                sendAll.setSendType(sendType);
                sendMessageMgr.sendMessage(MqType.HOTELSENDALL, sendAll, appKey);
            }
        }
    }


    @Override
    public boolean delRp(Integer storeId, Integer groupId, Integer opOurRateplanId) {
        List<HtlGroupEntity> opHtlGroups = opHtlGroupService.getOpHtlGroupHtLogicRoomByGIdAndSId(groupId, storeId);
        if(opHtlGroups!=null) {
            // 执行酒店总数量
            Long num = Long.valueOf(opHtlGroups.size());

            StoreEntity dtStore = dtStoreService.getById(storeId);
            String appKey = dtStore.getAppkey();

            // 生成新的任务
            TaskStatusEntity opTaskStatus = new TaskStatusEntity();
            opTaskStatus.setArchive(0);
            opTaskStatus.setTaskName(STORE_HOTEL_STATE_NAME);
            //完成数
            opTaskStatus.setCompPercent(0L);
            //总数
            opTaskStatus.setCompAmount(num);

            opTaskStatus.setStorId(storeId);
            opTaskStatus.setTaskCode(STORE_HOTEL_TASK_CODE);
            opTaskStatus.setStatus(0);
            opTaskStatus.setExcuTime(new Date());
            opTaskStatusService.save(opTaskStatus);
            Long taskId = opTaskStatus.getId();  // 任务id
            for (HtlGroupEntity opHtlGroup:opHtlGroups) {
                List<HtLogicRoomEntity> htLogicRooms = opHtlGroup.getHtLogicRooms();
                for (HtLogicRoomEntity htLogicRoom:htLogicRooms) {
                    DelRpMQ delRpMQ = new DelRpMQ();
                    Long rpId = htLogicRoom.getId();
                    delRpMQ.setRpId(rpId);
                    delRpMQ.setTaskId(taskId);
                    delRpMQ.setOpOurRateplanId(opOurRateplanId);
                    sendMessageMgr.sendMessage(MqType.DELRP, delRpMQ, appKey);
                }
            }
            DelRpMQ delRpMQ = new DelRpMQ();
            delRpMQ.setRpId((long) -1);
            sendMessageMgr.sendMessage(MqType.DELRP, delRpMQ, appKey);
            return true;
        }
        return true;
    }
}
